<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border='false'>
      <div slot="title" class="text">我的频道</div>
      <!-- 默认插槽显示在右边 -->
      <van-button round size="mini" type="danger" plain class="edit-btn" @click="isEditShow=!isEditShow">{{isEditShow?'完成':'编辑'}}</van-button>
    </van-cell>
    <!-- 宫格布局 -->
    <van-grid :gutter="10">
      <van-grid-item
        v-for="(channelItem,index) in mychannel"
        :key="index"
        class="channel-item"
        @click="editMyChannel(channelItem,index)"
      >
        <!-- 文字高亮显示 -->
        <span slot="text" class="text" :class="{active:active===index}">{{channelItem.name}}</span>
        <van-icon
          slot="icon"
          name="close"
          size="mini"
          v-show="isEditShow && !fixChannels.includes(channelItem.id)"
        ></van-icon>
      </van-grid-item>
    </van-grid>

    <!-- 推荐频道 -->
    <van-cell :border='false'>
      <div slot="title" class="text">推荐频道</div>
    </van-cell>
    <!-- 宫格布局 -->
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="recommendChannels in recommendChannels"
        :key="recommendChannels.id"
        icon="plus"
        @click="addChannel(recommendChannels)"
      >
        <span slot="text" class="text">{{recommendChannels.name}}</span>
      </van-grid-item>
    </van-grid>
  </div>
</template>

<script>
// 获取全部频道的接口
import { getAllChannels } from '@/api/channels'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    mychannel: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      // 全部频道列表
      allChannels: [],
      isEditShow: false,
      // 表示不删除的索引
      fixChannels: [0]
    }
  },
  created () {
    this.getAllChannels()
  },
  mounted () {},
  methods: {
    async getAllChannels () {
      try {
        const { data } = await getAllChannels()
        // console.log(data)
        this.allChannels = data.data.channels
      } catch (error) {
        this.$toast('获取频道列表失败，请稍后再试')
      }
    },
    // 给我的频道添加频道
    addChannel (recommendChannels) {
      this.mychannel.push(recommendChannels)
    },
    // 编辑频道
    editMyChannel (channelItem, index) {
      if (this.isEditShow) {
        // 在编辑状态，删除
        if (this.fixChannels.includes(channelItem.id)) {
          return
        }
        this.mychannel.splice(index, 1)
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1)
        }
      } else {
        // 不在编辑状态，跳转页面
        this.$emit('update-active', index)
      }
    }
  },
  // 计算属性，封装一些复杂的算法
  computed: {
    recommendChannels () {
      return this.allChannels.filter(channel => {
        return !this.mychannel.find(mychannels => {
          return channel.id === mychannels.id
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding-top: 85px;
  .text{
    font-size: 32px;
    color: #333333;
  }
  .edit-btn{
    width: 104px;
    height: 48px;
    font-size: 26px;
  }
  .channel-item {
    height: 86px;
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text,.text {
        color: #222;
        font-size: 28px;
        white-space: nowrap;
      }
      .active{
        color: red;
      }
      .van-grid-item__icon-wrapper{
        position: unset;
    }
    }

  }
  /deep/.recommend-grid{
    .van-grid-item__content{
      flex-direction: row;
      color:#222;
      .van-icon{
        font-size: 26px;
        margin-right: 5px;
      }
      .van-grid-item__text {
            font-size: 28px;
            margin-top: 0;
        }
    }
  }
  /deep/ .van-icon-close {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 36px;
    color: #ccc;
}
}
</style>
